package com.qili.controller;

import com.qili.core.utils.DictTypes;
import com.qili.entity.base.BaseDict;
import com.qili.entity.sys.SysRole;
import com.qili.entity.sys.dto.SysUserDTO;
import com.qili.service.SysUserService;
import com.qili.service.base.BaseDictService;
import com.qili.service.sys.RoleService;
import com.qili.util.DateUtil;
import com.qili.util.ExcelExportUtil;
import com.qili.util.JsonUtil;
import io.swagger.models.auth.In;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * @author zhangxiaopeng
 * @date 2021年11月3日
 */
@RequestMapping("/search/userRoles")
@Controller
public class UserRolesController {

    @Autowired
    SysUserService userService;

    @Autowired
    RoleService roleService;

    @Autowired
    BaseDictService baseDictService;


    /**
     * 用户角色导出
     * @param dto
     * @param response
     */
    @RequestMapping("exportUserRoles")
    @RequiresPermissions("user:expRoles")
    public void exportUserRoles(SysUserDTO dto, HttpServletResponse response){
        HashMap map=new HashMap<>();
        String fileName="用户角色"+ DateUtil.getCurrentDateNoSpliter();
        // 查询出所有的角色名称和对应的编码【表头】
        List<SysRole> roleList = roleService.selectAll();
        // 根据条件查询出需要导出的用户
        if (!StringUtils.isEmpty(dto.getDeptName())){
            List<String> list = new ArrayList<>();
            list.add(dto.getDeptName());
            dto.setDeptCodes(list);
        }
        List<SysUserDTO> userList = userService.selectExportUsers(dto);
        List<String> list;
        for (SysUserDTO sysUserDTO : userList) {
            list = this.regroupData(roleList,sysUserDTO.getOwnRoles());
            sysUserDTO.setOwnRolesCode(list);
        }
        map.put("name","用户角色");
        map.put("roleList",roleList);
        map.put("userList",userList);
        try {
            ExcelExportUtil.exportExcel(response,map,fileName,"66106");
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 查看所拥有的权限【把拥有的角色和表头中的角色对应起来】
     * @param roleList
     * @param ownRoles
     * @return
     */
    private List<String> regroupData(List<SysRole> roleList, List<SysRole> ownRoles){
        // 临时集合，存放角色编码
        List<String> tempRole = new ArrayList<>();
        for (SysRole role : roleList) {
            tempRole.add(role.getRoleName());
        }
        List<String> ownRolesCode = new ArrayList<>();
        // 定长数组-多少个角色就多大
        String[] arr = new String[roleList.size()];
        // 当前人拥有的角色
        for (SysRole ownRole : ownRoles) {
            int index = tempRole.indexOf(ownRole.getRoleName());
            // 不存在直接跳过
            if (index == -1){
                continue;
            }
            arr[index] = "1";
        }
        // 数组转为集合
        ownRolesCode = new ArrayList(Arrays.asList(arr));
        return ownRolesCode;
    }

    /**
     * 查询所有部门编号
     * @return
     */
    @GetMapping(value = "selectDepartment")
    @ResponseBody
    public JsonUtil selectDepartment(){
        BaseDict deptDict = new BaseDict();
        deptDict.setBadtTypecode(DictTypes.DEPT.getType());
        List<BaseDict> deptList = baseDictService.select(deptDict);
        if (deptList != null && deptList.size() > 0){
            return JsonUtil.sucessWithData("成功",deptList);
        }
        return JsonUtil.error("暂无部门信息");
    }
}
